package com.yanqu.road.server.pb;

import com.yanqu.road.entity.wingroom.chakra.WingRoomChakraPatronsAddItem;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;
import com.yanqu.road.pb.WingRoomChakraProto.WingRoomChakraProto;
import com.yanqu.road.server.manger.wingroom.WingRoomChakraMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.checkerframework.checker.units.qual.A;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：cwj
 * @date ：2023/10/13 15:27
 */
public class WingRoomChakraPb {

    /**
     * 命盘基础数据
     * @param userWingRoomChakra
     * @return
     */
    public static WingRoomChakraProto.WingRoomChakraData.Builder buildBaseChakraData(UserWingRoomChakra userWingRoomChakra) {
        WingRoomChakraProto.WingRoomChakraData.Builder builder = WingRoomChakraProto.WingRoomChakraData.newBuilder();
        builder.setWindWater(userWingRoomChakra.getRealWindWater());
        builder.setWindWaterLevel(userWingRoomChakra.getWindWaterLevel());
        int visitAddRate = WingRoomChakraMgr.getVisitWindWaterAddRate(userWingRoomChakra.getUserId());
        builder.setVisitAddRate(visitAddRate);
        builder.setChakraExp(userWingRoomChakra.getChakraExp());
        builder.setChakraLevel(userWingRoomChakra.getChakraLevel());
        for (UserWingRoomChakraDetail item : userWingRoomChakra.getWaitChakraDetail()) {
            builder.addWaitChakraItemList(builderChakraItem(item));
        }
        builder.setDrawNum(userWingRoomChakra.getDrawNum());
        return builder;
    }

    /**
     * 命盘详情数据
     * @param userWingRoomChakra
     * @param userWingRoomChakraDetailMap
     * @return
     */
    public static WingRoomChakraProto.WingRoomChakraData.Builder buildChakraData(UserWingRoomChakra userWingRoomChakra, Map<Integer, Map<Integer, Map<Integer, UserWingRoomChakraDetail>>> userWingRoomChakraDetailMap, Map<String, Integer> dailyVisitData, int bfFlag) {
        WingRoomChakraProto.WingRoomChakraData.Builder builder = buildBaseChakraData(userWingRoomChakra);
        //命盘【命格】【四象】数据
        List<WingRoomChakraProto.WingRoomChakraItem> itemList = buildChakraDetailList(userWingRoomChakraDetailMap);
        builder.addAllWingRoomChakraItem(itemList);
        //每次拜访次数数据
        if (dailyVisitData != null) {
            builder.addAllVisitNumList(buildVisitNumList(dailyVisitData));
        }
        builder.setBfFlag(bfFlag);
        return builder;
    }

    /**
     * build 每次拜访次数数据
     * @param dailyVisitData
     * @return
     */
    public static List<WingRoomChakraProto.WingRoomChakraVisitUserNumItem> buildVisitNumList(Map<String, Integer> dailyVisitData) {
        List<WingRoomChakraProto.WingRoomChakraVisitUserNumItem> list = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : dailyVisitData.entrySet()) {
            long userId = Long.parseLong(entry.getKey());
            int visitNum = entry.getValue();
            WingRoomChakraProto.WingRoomChakraVisitUserNumItem.Builder builder = WingRoomChakraProto.WingRoomChakraVisitUserNumItem.newBuilder();
            builder.setUserId(userId);
            builder.setVisitNum(visitNum);
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 命盘【命格】【四象】数据
     * @param userWingRoomChakraDetailMap
     * @return
     */
    public static List<WingRoomChakraProto.WingRoomChakraItem> buildChakraDetailList(Map<Integer, Map<Integer, Map<Integer, UserWingRoomChakraDetail>>> userWingRoomChakraDetailMap) {
        List<WingRoomChakraProto.WingRoomChakraItem> list = new ArrayList<>();
        for (Map<Integer, Map<Integer, UserWingRoomChakraDetail>> typeMap : userWingRoomChakraDetailMap.values()) {
            for (Map<Integer, UserWingRoomChakraDetail> positionMap : typeMap.values()) {
                for (UserWingRoomChakraDetail userWingRoomChakraDetail : positionMap.values()) {
                    WingRoomChakraProto.WingRoomChakraItem.Builder chakraItem = builderChakraItem(userWingRoomChakraDetail);
                    list.add(chakraItem.build());
                }
            }
        }
        return list;
    }

    /**
     * 四象，命格数据
     * @param userWingRoomChakraDetail
     * @return
     */
    public static WingRoomChakraProto.WingRoomChakraItem.Builder builderChakraItem(UserWingRoomChakraDetail userWingRoomChakraDetail) {
        WingRoomChakraProto.WingRoomChakraItem.Builder item =  WingRoomChakraProto.WingRoomChakraItem.newBuilder();
        item.setOccupation(userWingRoomChakraDetail.getOccupation());
        item.setType(userWingRoomChakraDetail.getType());
        item.setPosition(userWingRoomChakraDetail.getPosition());
        item.setWxType(userWingRoomChakraDetail.getWxType());
        item.setLevel(userWingRoomChakraDetail.getLevel());
        item.setQuality(userWingRoomChakraDetail.getQuality());
        for (WingRoomChakraPatronsAddItem patronsAddItem : userWingRoomChakraDetail.getPatronsList()) {
            WingRoomChakraProto.WingRoomChakraPatronsAddItem.Builder patronsAddItemPb = builderPatronsAddItem(patronsAddItem);
            item.addPatronsAddData(patronsAddItemPb);
        }
        return item;
    }

    /**
     * 门客加成信息
     * @param patronsAddItem
     * @return
     */
    public static WingRoomChakraProto.WingRoomChakraPatronsAddItem.Builder builderPatronsAddItem(WingRoomChakraPatronsAddItem patronsAddItem) {
        WingRoomChakraProto.WingRoomChakraPatronsAddItem.Builder builder =  WingRoomChakraProto.WingRoomChakraPatronsAddItem.newBuilder();
        builder.setPatronsId(patronsAddItem.getPatronsId());
        builder.setType(patronsAddItem.getType());
        builder.setAddValue(patronsAddItem.getAddValue());
        builder.setAddAbility(patronsAddItem.getAddAbility());
        return builder;
    }
}
